const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const TradingPositions = sequelize.define('TradingPositions', {
  id: {
    type: DataTypes.BIGINT,
    allowNull: false,
    autoIncrement: true,
    comment: '仓位ID',
  },
  userId: {
    type: DataTypes.BIGINT,
    allowNull: false,
    comment: '用户ID',
  },
  strategyId: {
    type: DataTypes.BIGINT,
    comment: '策略ID',
  },
  symbol: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: '交易对',
  },
  side: {
    type: DataTypes.ENUM,
    allowNull: false,
    comment: '仓位方向',
  },
  quantity: {
    type: DataTypes.DECIMAL(20, 8),
    allowNull: false,
    comment: '仓位数量',
  },
  avgPrice: {
    type: DataTypes.DECIMAL(20, 8),
    allowNull: false,
    comment: '平均价格',
  },
  unrealizedPnl: {
    type: DataTypes.DECIMAL(20, 8),
    defaultValue: 0.00000000,
    comment: '未实现盈亏',
  },
  realizedPnl: {
    type: DataTypes.DECIMAL(20, 8),
    defaultValue: 0.00000000,
    comment: '已实现盈亏',
  },
  margin: {
    type: DataTypes.DECIMAL(20, 8),
    defaultValue: 0.00000000,
    comment: '保证金',
  },
  leverage: {
    type: DataTypes.INTEGER,
    defaultValue: 1,
    comment: '杠杆倍数',
  },
  status: {
    type: DataTypes.ENUM,
    defaultValue: open,
    comment: '仓位状态',
  },
  closedAt: {
    type: DataTypes.DATE,
    comment: '平仓时间',
  },
  deletedAt: {
    type: DataTypes.DATE,
    comment: '删除时间',
  },
  createdAt: {
    type: DataTypes.DATE,
    defaultValue: CURRENT_TIMESTAMP,
    comment: '创建时间',
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: CURRENT_TIMESTAMP,
    comment: '更新时间',
  }
}, {
  tableName: 'trading_positions',
  comment: '交易仓位表',
  timestamps: true,
  paranoid: true,
  underscored: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  deletedAt: 'deleted_at'
});

module.exports = TradingPositions;
